package com.csair.seam.controller.pc;


import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.csair.seam.domain.model.entity.VisitStatistics;
import com.csair.seam.domain.model.enums.VisitsModule;
import com.csair.seam.domain.model.param.VisitsParam;
import com.csair.seam.domain.model.vo.VisitsVo;
import com.csair.seam.domain.service.IVisitsService;
import com.csair.seam.infrastructure.log.LogOperation;
import com.csair.seam.infrastructure.utils.DateHelper;
import com.csair.seam.infrastructure.web.BaseController;
import com.csair.seam.infrastructure.web.BaseResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "访问量服务接口")
@RestController
@RequestMapping("/pc/visits")
public class VisitsController extends BaseController {

	@Autowired
    private IVisitsService visitsService;
	
	@ApiOperation(value = "访问量监控", notes = "getVisitsList",httpMethod="POST")
    @PostMapping(value = "/list")
    @LogOperation(bizName = "查询访问量")
    public BaseResponse<List<VisitsVo>> getVisitsList(@ApiParam(value = "查询参数")VisitsParam param){
		if(StringUtils.isEmpty(param.getStartDate())) {
			param.setStartDate(DateHelper.getCurrentDateStr());
		}
		if(StringUtils.isEmpty(param.getEndDate())) {
			param.setEndDate(DateHelper.getCurrentDateStr());
		}
		List<VisitStatistics> moduleVisitsList = visitsService.findModuleVisitsList(param);
		Map<String, VisitsVo> map = new HashMap<>();
		for (VisitStatistics visits : moduleVisitsList) {
			VisitsVo vo = map.get(visits.getOrgId());
			if(vo == null) {
				vo = new VisitsVo();
				vo.setOrgId(visits.getOrgId());
				vo.setOrgName(visits.getOrgName());
			}
			if(visits.getModuleCode().equals(VisitsModule.MY_EQUIP_QUERY.getCode())) {
				vo.setMyEquipQueryUserNum(visits.getUserNum());
				vo.setMyEquipQueryVisitNum(visits.getVisitNum());
			}else if(visits.getModuleCode().equals(VisitsModule.ASSET_INVENTORY_QUERY.getCode())) {
				vo.setAssetInventoryQueryUserNum(visits.getUserNum());
				vo.setAssetInventoryQueryVisitNum(visits.getVisitNum());
			}else if(visits.getModuleCode().equals(VisitsModule.ASSET_INVENTORY_MATCH.getCode())) {
				vo.setAssetInventoryMatchUserNum(visits.getUserNum());
				vo.setAssetInventoryMatchVisitNum(visits.getVisitNum());
			}else if(visits.getModuleCode().equals(VisitsModule.ASSET_INVENTORY_UNMATCH.getCode())) {
				vo.setAssetInventoryUnMatchUserNum(visits.getUserNum());
				vo.setAssetInventoryUnMatchVisitNum(visits.getVisitNum());
			}else if(visits.getModuleCode().equals(VisitsModule.ASSET_INVENTORY_REVOKE.getCode())) {
				vo.setAssetInventoryRevokeUserNum(visits.getUserNum());
				vo.setAssetInventoryRevokeVisitNum(visits.getVisitNum());
			}else if(visits.getModuleCode().equals(VisitsModule.SHARING_CENTER_QUERY.getCode())) {
				vo.setSharingCenterQueryUserNum(visits.getUserNum());
				vo.setSharingCenterQueryVisitNum(visits.getVisitNum());
			}else if(visits.getModuleCode().equals(VisitsModule.SHARING_CENTER_MYFOLLOW.getCode())) {
				vo.setSharingCenterMyFollowUserNum(visits.getUserNum());
				vo.setSharingCenterMyFollowVisitNum(visits.getVisitNum());
			}
			
			map.put(visits.getOrgId(), vo);
		}
		if(map.isEmpty()) {
			return buildSuccess(Collections.emptyList());
		}
		List<VisitsVo> data = map.values().stream().collect(Collectors.toList());
		for (VisitsVo visitsVo : data) {
			int myEquipUserNum = visitsVo.getMyEquipQueryUserNum();
			visitsVo.setMyEquipUserNum(myEquipUserNum);
			int myEquipVisitNum =  visitsVo.getMyEquipQueryVisitNum();
			visitsVo.setMyEquipVisitNum(myEquipVisitNum);
			
			int assetInventoryUserNum = visitsVo.getAssetInventoryQueryUserNum()
					+visitsVo.getAssetInventoryMatchUserNum()
					+visitsVo.getAssetInventoryUnMatchUserNum()
					+visitsVo.getAssetInventoryRevokeUserNum();
			visitsVo.setAssetInventoryUserNum(assetInventoryUserNum);
			int assetInventoryVisitNum = visitsVo.getAssetInventoryQueryVisitNum()
					+visitsVo.getAssetInventoryMatchVisitNum()
					+visitsVo.getAssetInventoryUnMatchVisitNum()
					+visitsVo.getAssetInventoryRevokeVisitNum();
			visitsVo.setAssetInventoryVisitNum(assetInventoryVisitNum);
			
			int sharingCenterUserNum = visitsVo.getSharingCenterQueryUserNum()+visitsVo.getSharingCenterMyFollowUserNum();
			visitsVo.setSharingCenterUserNum(sharingCenterUserNum);
			int sharingCenterVisitNum = visitsVo.getSharingCenterVisitNum()+visitsVo.getSharingCenterQueryVisitNum()+
					visitsVo.getSharingCenterMyFollowVisitNum();
			visitsVo.setSharingCenterVisitNum(sharingCenterVisitNum);
			
			int totalUserNum = visitsVo.getMyEquipUserNum()+visitsVo.getAssetInventoryUserNum()+visitsVo.getSharingCenterUserNum();
			int totalVisitNum = visitsVo.getMyEquipVisitNum()+visitsVo.getAssetInventoryVisitNum()+visitsVo.getSharingCenterVisitNum();
			visitsVo.setTotalUserNum(totalUserNum);
			visitsVo.setTotalVisitNum(totalVisitNum);
			visitsVo.setAvgUv(Math.round((float)totalVisitNum/3));
		}
		return buildSuccess(data);
    }
	
}

